<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="课程名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入课程名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入课程价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学习人数" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder="请输入学习人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:course:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:course:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:course:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:course:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="courseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="课程名" align="center" prop="name"/>
      <el-table-column label="课程描述" align="center" prop="content"/>
      <el-table-column label="课程价格" align="center" prop="price"/>
      <el-table-column label="学习人数" align="center" prop="people"/>
      <el-table-column label="课程封面" align="center" prop="picture">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="课程图标" align="center" prop="icon">
        <template slot-scope="scope">
          <image-preview :src="scope.row.icon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['project:course:edit']"
          >课程详情
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:course:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:course:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名"/>
        </el-form-item>
        <el-form-item label="课程描述" prop="content">
          <el-input
            v-model="form.content"
            placeholder="请输入内容"
            :show-overflow-tooltip="true"
          />
        </el-form-item>
        <el-form-item label="课程价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入课程价格"/>
        </el-form-item>
        <!-- <el-form-item label="学习人数" prop="people">
          <el-input v-model="form.people" placeholder="请输入学习人数" />
        </el-form-item> -->
        <el-form-item label="课程封面" prop="picture">
          <image-upload v-model="form.picture"/>
        </el-form-item>
        <el-form-item label="课程图标" prop="icon">
          <image-upload v-model="form.icon"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改课程详情对话框 -->
    <el-dialog
      title="课程详情"
      :visible.sync="openDetail"
      width="650px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="detailForm"
        :model="detailForm"
        :rules="detailRules"
        label-width="100px"
      >
        <el-form-item label="课程id" prop="courseId" v-show="false">
          <el-input v-model="detailForm.courseId" placeholder="请输入课程id"/>
        </el-form-item>
        <el-form-item label="课程名" prop="courseName">
          <el-input
            v-model="detailForm.courseName"
            placeholder="请输入课程名"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="课程评分" prop="mark">
          <el-input v-model="detailForm.mark" placeholder="请输入课程评分"/>
        </el-form-item>
        <el-form-item label="教学目标" prop="target">
          <el-input
            v-model="detailForm.target"
            type="textarea"
            :autosize="{ minRows: 3 }"
            placeholder="请输入教学目标"
          />
        </el-form-item>
        <el-form-item label="教学目标配图" prop="targetImage">
          <image-upload v-model="detailForm.targetImage"/>
        </el-form-item>
        <el-form-item label="级别描述" prop="level">
          <el-input
            v-model="detailForm.level"
            type="textarea"
            :autosize="{ minRows: 3 }"
            placeholder="请输入级别描述"
          />
        </el-form-item>
        <el-form-item label="级别描述配图" prop="levelImage">
          <image-upload v-model="detailForm.levelImage"/>
        </el-form-item>
      </el-form>
      <div style="width: 100%; text-align: center">
        <el-button type="primary" @click="submitDetailForm" style="width: 40%"
        >确 定
        </el-button
        >
        <el-button @click="openDetail = false" style="width: 40%"
        >取 消
        </el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCourse,
  getCourse,
  delCourse,
  addCourse,
  updateCourse,
} from "./api.js";
import {
  listCourseDetail,
  addCourseDetail,
  updateCourseDetail,
} from "../courseDetail/api.js";
import {getToken} from "@/utils/auth";

export default {
  name: "Course",
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/project/course/import",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示课程详情弹出层
      openDetail: false,
      // 课程名
      courseName: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        courseId: null,
        content: null,
        picture: null,
        icon: null,
        price: null,
        people: null,
        remark: null,
      },
      // 表单参数
      form: {},
      // 详情表单参数
      detailForm: {},
      // 表单校验
      rules: {
        name: [{required: true, message: "课程名不能为空", trigger: "blur"}],
        content: [
          {required: true, message: "课程描述不能为空", trigger: "blur"},
        ],
        price: [
          {required: true, message: "课程价格不能为空", trigger: "blur"},
        ],
      },
      detailRules: {
        target: [
          {required: true, message: "教学目标不能为空", trigger: "blur"},
        ],
        level: [
          {required: true, message: "级别描述不能为空", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        content: null,
        picture: null,
        icon: null,
        price: null,
        people: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.detailForm = {
        id: null,
        courseId: null,
        courseName: null,
        mark: null,
        target: null,
        targetImage: null,
        level: null,
        levelImage: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      this.queryParams.courseId = id;
      listCourseDetail(this.queryParams).then((response) => {
        this.openDetail = true;
        if (response.rows.length === 0) {
          this.detailForm.courseId = id;
          this.detailForm.courseName = row.name;
        } else {
          this.detailForm = response.rows[0];
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCourse(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 提交课程详情 */
    submitDetailForm() {
      if (this.detailForm.id != null) {
        updateCourseDetail(this.detailForm).then((response) => {
          this.openDetail = false;
          this.getList();
        });
      } else {
        addCourseDetail(this.detailForm).then((response) => {
          this.openDetail = false;
          this.getList();
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then((response) => {
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then((response) => {
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCourse(ids);
        })
        .then(() => {
          this.getList();
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "project/course/export",
        {
          ...this.queryParams,
        },
        `course_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入excel";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "project/course/importTemplate",
        {},
        `course_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
